﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , Aite TECH, Ltd.
//--------------------------------------------------------------------

using System.Data;
using System.Collections.Generic;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.Utilities;
  

     /// <summary>
     /// CiParameterManager
     /// 系统参数配置表
     ///
     /// 修改纪录
     ///
     ///		2012-03-02 版本：1.0 EricHu 创建主键。
     ///
     /// 版本：1.0
     ///
     /// <author>
     ///		<name>EricHu</name>
     ///		<date>2012-03-02</date>
     /// </author>
     /// </summary>
    public partial class CiParameterManager : DbCommonManager, IDbCommonManager
    {
          /// <summary>
          /// 构造函数
          /// </summary>
          public CiParameterManager()
          {
              base.CurrentTableName = CiParameterTable.TableName;
              base.PrimaryKey = "Id";
          }

          /// <summary>
          /// 构造函数
          /// <param name="tableName">指定表名</param>
          /// </summary>
          public CiParameterManager(string tableName)
          {
              base.CurrentTableName = tableName;
          }

          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="dbProvider">数据库连接</param>
          public CiParameterManager(IDbProvider dbProvider): this()
          {
              DBProvider = dbProvider;
          }

          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="userInfo">用户信息</param>
          public CiParameterManager(UserInfo userInfo) : this()
          {
              UserInfo = userInfo;
          }

          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="dbProvider">数据库连接</param>
          /// <param name="userInfo">用户信息</param>
          public CiParameterManager(IDbProvider dbProvider, UserInfo userInfo) : this(dbProvider)
          {
              UserInfo = userInfo;
          }

          /// <summary>
          /// 构造函数
          /// </summary>
          /// <param name="dbProvider">数据库连接</param>
          /// <param name="userInfo">用户信息</param>
          /// <param name="tableName">指定表名</param>
          public CiParameterManager(IDbProvider dbProvider, UserInfo userInfo, string tableName) : this(dbProvider, userInfo)
          {
              base.CurrentTableName = tableName;
          }

   
          /// <summary>
          /// 添加
          /// </summary>
          /// <param name="ciParameterEntity">实体</param>
          /// <param name="identity">自增量方式</param>
          /// <param name="returnId">返回主键</param>
          /// <returns>主键</returns>
          public string Add(CiParameterEntity ciParameterEntity, bool identity, bool returnId)
          {
              this.Identity = identity;
              this.ReturnId = returnId;
              return this.AddEntity(ciParameterEntity);
          }

          /// <summary>
          /// 获取实体
          /// </summary>
          /// <param name="id">主键</param>
          public CiParameterEntity GetEntity(string id)
          {
              return GetEntity(int.Parse(id));
          }

          public CiParameterEntity GetEntity(int id)
          {
              CiParameterEntity ciParameterEntity = new CiParameterEntity(this.GetDT(CiParameterTable.FieldId, id));
              return ciParameterEntity;
          }

          /// <summary>
          /// 添加实体
          /// </summary>
          /// <param name="ciParameterEntity">实体</param>
          public string AddEntity(CiParameterEntity ciParameterEntity)
          {
               string sequence = string.Empty;
              if (ciParameterEntity.SortCode == null || ciParameterEntity.SortCode == 0)
              {
                  CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                  sequence = sequenceManager.GetSequence(this.CurrentTableName);
                  ciParameterEntity.SortCode = int.Parse(sequence);
              }
              SQLBuilder sqlBuilder = new SQLBuilder(DBProvider, this.Identity, this.ReturnId);
              sqlBuilder.BeginInsert(this.CurrentTableName, CiParameterTable.FieldId);

              if (!this.Identity)
              {
                  sqlBuilder.SetValue(CiParameterTable.FieldId, ciParameterEntity.Id);
              }
              else
              {
                  if (!this.ReturnId && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                  {
                      if (DBProvider.CurrentDbType == CurrentDbType.Oracle)
                      {
                          sqlBuilder.SetFormula(CiParameterTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
                      }
                      if (DBProvider.CurrentDbType == CurrentDbType.DB2)
                      {
                          sqlBuilder.SetFormula(CiParameterTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
                      }
                  }
                  else
                  {
                      if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                      {
                          if (ciParameterEntity.Id == null)
                          {
                              if (string.IsNullOrEmpty(sequence))
                              {
                                  CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                                  sequence = sequenceManager.GetSequence(this.CurrentTableName);
                              }
                              ciParameterEntity.Id = int.Parse(sequence);
                          }
                          sqlBuilder.SetValue(CiParameterTable.FieldId, ciParameterEntity.Id);
                      }
                  }
              }

              this.SetEntity(sqlBuilder, ciParameterEntity);
              if (UserInfo != null)
              {
                  sqlBuilder.SetValue(CiParameterTable.FieldCreateUserId, UserInfo.Id);
                  sqlBuilder.SetValue(CiParameterTable.FieldCreateBy, UserInfo.RealName);
              }
              sqlBuilder.SetDBNow(CiParameterTable.FieldCreateOn);
              if (UserInfo != null)
              {
                  sqlBuilder.SetValue(CiParameterTable.FieldModifyUserId, UserInfo.Id);
                  sqlBuilder.SetValue(CiParameterTable.FieldModifiedBy, UserInfo.RealName);
              } 
              sqlBuilder.SetDBNow(CiParameterTable.FieldModifiedOn);

              if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.SqlServer || DBProvider.CurrentDbType == CurrentDbType.Access))
              {
                  sequence = sqlBuilder.EndInsert().ToString();
              }
              else
              {
                  sqlBuilder.EndInsert();
              }
            return sequence;
          }

          /// <summary>
          /// 更新实体
          /// </summary>
          /// <param name="ciParameterEntity">实体</param>
          public int UpdateEntity(CiParameterEntity ciParameterEntity)
          {
              SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
              sqlBuilder.BeginUpdate(this.CurrentTableName);
              this.SetEntity(sqlBuilder, ciParameterEntity);
              if (UserInfo != null) 
              { 
              sqlBuilder.SetValue(CiParameterTable.FieldModifiedBy, UserInfo.RealName);
              } 
              sqlBuilder.SetDBNow(CiParameterTable.FieldModifiedOn);
              sqlBuilder.SetWhere(CiParameterTable.FieldId, ciParameterEntity.Id);
              return sqlBuilder.EndUpdate();
          }

          /// <summary>
          /// 设置实体
          /// </summary>
          /// <param name="ciParameterEntity">实体</param>
          private void SetEntity(SQLBuilder sqlBuilder, CiParameterEntity ciParameterEntity)
          {
              sqlBuilder.SetValue(CiParameterTable.FieldCategoryId, ciParameterEntity.CategoryId);
              sqlBuilder.SetValue(CiParameterTable.FieldParameterId, ciParameterEntity.ParameterId);
              sqlBuilder.SetValue(CiParameterTable.FieldParameterCode, ciParameterEntity.ParameterCode);
              sqlBuilder.SetValue(CiParameterTable.FieldParameterContent, ciParameterEntity.ParameterContent);
              sqlBuilder.SetValue(CiParameterTable.FieldWorked, ciParameterEntity.Worked);
              sqlBuilder.SetValue(CiParameterTable.FieldDescription, ciParameterEntity.Description);
              sqlBuilder.SetValue(CiParameterTable.FieldSortCode, ciParameterEntity.SortCode);
              sqlBuilder.SetValue(CiParameterTable.FieldDeleteMark, ciParameterEntity.DeleteMark);
          }

          /// <summary>
          /// 删除实体
          /// </summary>
          /// <param name="id">主键</param>
          /// <returns>影响行数</returns>
          public int Delete(int id)
          {
              return this.Delete(CiParameterTable.FieldId, id);
          }

          #region public string Add(string categoryId, string parameterId, string parameterCode, string parameterContent, bool worked, bool enabled)
          /// <summary>
          /// 添加
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="parameterId">标记主键</param>
          /// <param name="parameterCode">标记编码</param>
          /// <param name="parameterContent">标记内容</param>
          /// <param name="worked">工作状态</param>
          /// <param name="enabled">有效</param>
          /// <returns>主键</returns>
          public string Add(string categoryId, string parameterId, string parameterCode, string parameterContent, bool worked, bool enabled)
          {
              CiParameterEntity parameterEntity = new CiParameterEntity();
              parameterEntity.CategoryId = categoryId;
              parameterEntity.ParameterId = parameterId;
              parameterEntity.ParameterCode = parameterCode;
              parameterEntity.ParameterContent = parameterContent;
              parameterEntity.Worked = worked ? 1:0;
              parameterEntity.Enabled = enabled ? 1 : 0;
              return this.Add(parameterEntity);
          }
          #endregion

          #region public string Add(CiParameterEntity parameterEntity)
          /// <summary>
          /// 添加内容
          /// </summary>
          /// <param name="parameterEntity">内容</param>
          /// <returns>主键</returns>
          public string Add(CiParameterEntity parameterEntity)
          {
              string returnValue = string.Empty;
              // 此处检查this.exist()
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, parameterEntity.CategoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterEntity.ParameterId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterEntity.ParameterCode));
              if (this.Exists(parameters))
              {
                  // 编号已重复
                  this.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
              }
              else
              {
                  returnValue = this.AddEntity(parameterEntity);
                  // 运行成功
                  this.ReturnStatusCode = StatusCode.OKAdd.ToString();
              }
              return returnValue;
          }
          #endregion

          #region public int Update(CiParameterEntity parameterEntity) 更新
          /// <summary>
          /// 更新
          /// </summary>
          /// <param name="parameterEntity">参数基类表结构定义</param>
          /// <returns>影响行数</returns>
          public int Update(CiParameterEntity parameterEntity)
          {
              int returnValue = 0;

              // 检查编号是否重复
              if (this.Exists(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterEntity.ParameterCode), parameterEntity.Id))
              {
                  // 文件夹名已重复
                  this.ReturnStatusCode = StatusCode.ErrorCodeExist.ToString();
              }
              else
              {
                  // 进行更新操作
                  returnValue = this.UpdateEntity(parameterEntity);
                  if (returnValue == 1)
                  {
                      this.ReturnStatusCode = StatusCode.OKUpdate.ToString();
                  }
                  else
                  {
                      // 数据可能被删除
                      this.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
                  }
              }
              return returnValue;
          }
          #endregion

          #region public string GetParameter(string categoryId, string parameterId, string parameterCode)
          /// <summary>
          /// 获取参数
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="flagID">标志主键</param>
          /// <param name="paramFlagCode">编码</param>
          /// <returns>参数值</returns>
          public string GetParameter(string categoryId, string parameterId, string parameterCode)
          {
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterCode));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldDeleteMark, 0));
              return this.GetProperty(parameters, CiParameterTable.FieldParameterContent).ToString();
          }
          #endregion

          #region public int SetParameter(string categoryId, string parameterId, string parameterCode, string parameterContent)
          /// <summary>
          /// 更新参数设置
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="parameterId">标志主键</param>
          /// <param name="parameterCode">编码</param>
          /// <param name="parameterContent">参数内容</param>
          /// <returns>影响行数</returns>
          public int SetParameter(string categoryId, string parameterId, string parameterCode, string parameterContent)
          {
              int returnValue = 0;

              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterCode));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldDeleteMark, 0));
              // 检测是否无效数据
              if ((parameterContent == null) || (parameterContent.Length == 0))
              {
                  returnValue = this.Delete(parameters);
              }
              else
              {
                  // 检测是否存在
                  returnValue = this.SetProperty(parameters, new KeyValuePair<string, object>(CiParameterTable.FieldParameterContent, parameterContent));
                  if (returnValue == 0)
                  {
                      // 进行增加操作
                      CiParameterEntity parameterEntity = new CiParameterEntity();
                      parameterEntity.CategoryId = categoryId;
                      parameterEntity.ParameterId = parameterId;
                      parameterEntity.ParameterCode = parameterCode;
                      parameterEntity.ParameterContent = parameterContent;
                      parameterEntity.Enabled = 1;
                      parameterEntity.DeleteMark = 0;
                      this.AddEntity(parameterEntity);
                      returnValue = 1;
                  }
              }
              return returnValue;
          }
          #endregion

          #region public DataTable GetDataTableByParameter(string categoryId, string parameterId)
          /// <summary>
          /// 获取记录
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="flagID">标志主键</param>
          /// <param name="paramFlagCode">编码</param>
          /// <returns>数据表</returns>
          public DataTable GetDataTableByParameter(string categoryId, string parameterId)
          {
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              return this.GetDT(parameters);
          }
          #endregion

          #region public DataTable GetDataTableParameterCode(string categoryId, string parameterId, string parameterCode)
          /// <summary>
          /// 获取记录
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="parameterId">标志主键</param>
          /// <param name="parameterCode">编码</param>
          /// <returns>数据表</returns>
          public DataTable GetDataTableParameterCode(string categoryId, string parameterId, string parameterCode)
          {
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterCode));
              return this.GetDT(parameters, CiParameterTable.FieldCreateOn);
          }
          #endregion

          #region public int DeleteByParameter(string categoryId, string parameterId)
          /// <summary>
          /// 删除
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="flagID">标志主键</param>
          /// <returns>影响行数</returns>
          public int DeleteByParameter(string categoryId, string parameterId)
          {
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              return this.Delete(parameters);
          }
          #endregion

          #region public int DeleteByParameterCode(string categoryId, string parameterId, string parameterCode)
          /// <summary>
          /// 删除
          /// </summary>
          /// <param name="categoryId">类别主键</param>
          /// <param name="parameterId">标志主键</param>
          /// <param name="parameterCode">标志编号</param>
          /// <returns>影响行数</returns>
          public int DeleteByParameterCode(string categoryId, string parameterId, string parameterCode)
          {
              List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldCategoryId, categoryId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterId, parameterId));
              parameters.Add(new KeyValuePair<string, object>(CiParameterTable.FieldParameterCode, parameterCode));
              return this.Delete(parameters);
          }
          #endregion
      }
}
